﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NFlickr.Resource;
using NFlickr.Core.Search;

namespace NFlickr.Core
{
    public class FlickrService
    {
        public FlickrService()
        {

        }
        public NFlickr.Resource.Permission Permission
        {
            get;
            set;
        }

        private string _applicationKey;
        public string ApplicationKey
        {
            get
            {
                return _applicationKey;
            }
            set
            {
                _applicationKey = value;
            }
        }

        private string _secret;
        public string Secret
        {
            get
            {
                return _secret;
            }
            set
            {
                _secret = value;
            }
        }

        private Frob _frob;
        public Frob Frob
        {
            get
            {
                if (_frob == null)
                {
                    _frob = new Frob { ApplicationKey = ApplicationKey, Secret = Secret };
                }
                return _frob;
            }

        }



        private Token _token;

        public Token Token
        {
            get
            {
                if (_token == null)
                {
                    _token = new Token { ApplicationKey = ApplicationKey, Secret = Secret, Frob = Frob };
                }
                return _token;
            }
        }


        private FlickrUrl _filckrUrl;

        public FlickrUrl FlickrUrl
        {
            get
            {
                if (_filckrUrl == null)
                {
                    _filckrUrl = new FlickrUrl
                    {
                        Frob = Frob,
                        Permission = Permission
                    };
                }
                return _filckrUrl;

            }

        }


        private IList<PhotoSet> _photoSets;

        public IList<PhotoSet> PhotoSetList
        {
            get 
            {

                if (_photoSets == null)
                {
                    PhotoSets photoSets = new PhotoSets(this);
                    _photoSets = photoSets.Items;
                }
                return _photoSets;
            }

        }


        public Photo CreatePhoto(bool isHidden, SafetyLevel safetyLevel, bool isPublic, bool isFriend, bool isFamily, IList<string> tags, string title, string description)
        {
            Photo photo = new Photo(this)
            {
                Hidden = isHidden,
                SafetyLevel = safetyLevel,
                Friend = isFriend,
                Public = isPublic,
                Tags = tags,
                Title = title,
                Description = description,
                Family = isFamily
            };
            return photo;
        }

        public PhotoSet CreatePhotoSet(string title, string description, Photo primaryPhoto)
        {
            PhotoSet photoSet = new PhotoSet(this)
            {
                Frob = Frob,
                Token = Token,
                PrimaryPhoto = primaryPhoto,
                Title = title,
                Description = description
            };
            return CreatePhotoSet(photoSet, primaryPhoto);
        }

        public PhotoSet CreatePhotoSet(PhotoSet photoSet, Photo primaryPhoto)
        {
            photoSet.PrimaryPhoto = primaryPhoto;
            if (photoSet.Create())
            {
                return photoSet;
            }
            return null;
        }

        public Photo Upload(Photo photo, string localPath)
        {
            Photo uploaded = photo;
            string photoid = photo.Upload(localPath);
            uploaded.PhotoId = photoid;
            return uploaded;
        }

        public IList<Photo> Search(PhotoSearch photoSearch)
        {
            IList<Photo> photoList = new List<Photo>();
            Photos photos = new Photos(this, photoSearch);
            photos.Refresh();
            return photos.PhotoList;
        }

    }
}
